﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using Common;
using WinUI.Frm_Program;
using WinUI.Frm_MasterPlan;
using WinUI.Frm_Quotation;
using WinUI.Frm_Administrator;

namespace WinUI.Frm
{
    public partial class MainFrm : DevExpress.XtraEditors.XtraForm
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            InitMenu();
            ShowInfo();
            HomeFrm frm = new HomeFrm();
            frm.MdiParent = this;
            frm.Show();
        }

        private void InitMenu()
        {
            Common.UserInfo userInfo = Global._UserInfo;
            string messageSystemError = String.Empty;
            Business.User user = new Business.User();
            List<Business.Menu> listMenu = new List<Business.Menu>();
            Common.ERROR_CODE.ERROR_CODE_SIMPLE errCode = user.GetUserMenu(ref messageSystemError, userInfo, ref listMenu, Business.Menu.WINFORM_TYPE);
            foreach (Business.Menu item in listMenu)
            {
                ToolStripMenuItem mn = new ToolStripMenuItem();
                mn.Text = item.Name;
                mn.Tag = item.Link;
                mainMenu.Items.Add(mn);
                mn.Click += mn_Click;
                InitSubMenu(mn, item.ListSubMenu);
            }
        }

        private void InitSubMenu(ToolStripMenuItem MenuItem, List<Business.Menu> ListMenu)
        {
            foreach (Business.Menu item in ListMenu)
            {
                ToolStripMenuItem mn = new ToolStripMenuItem();
                mn.Text = item.Name;
                mn.Tag = item.Link;
                MenuItem.DropDownItems.Add(mn);
                mn.Click += mn_Click;
                if (item.ListSubMenu.Count > 0)
                    InitSubMenu(mn, item.ListSubMenu);
            }
        }

        void mn_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem mn = sender as ToolStripMenuItem;
            string mnLink = mn.Tag.ToString();
            Form frm = new Form();
            switch (mnLink)
            {
                case Global.CreateProgramFrmName:
                    frm = new Frm_CreateProgram();
                    //frm.MdiParent = this;
                    frm.ShowDialog();
                    break;
                case Global.EditProgramFrmName:
                    frm = new Frm_EditProgram();
                    //frm.MdiParent = this;
                    frm.ShowDialog();
                    break;
                case Global.ViewProgramFrmName:
                    frm = new Frm_ViewProgram();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
                case Global.CreateMasterPlan:
                    frm = new Frm_CreateMasterPlan();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
                case Global.EditMasterPlan:
                    frm = new Frm_EditMasterPlan();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
                case Global.ViewMasterPlan:
                    frm = new Frm_ViewMasterPlan();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
                case Global.CreateQuotation:
                    frm = new Frm_CreateQuotation();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
                case Global.EditQuotation:
                    frm = new Frm_EditQuotation();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
                case Global.ViewQuotation:
                    frm = new Frm_ViewQuotation();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
                case Global.CostcardManagement:
                    frm = new Frm_CostcardManagement();
                    frm.MdiParent = this;
                    frm.Show();
                    break;
            }
        }

        private void ShowInfo()
        {
            toolStripStatusLabelUser.Text = String.Format("{0}", Global._UserInfo.UserName);
        }

        public void SetStatus(string Value)
        {
            toolStripStatusLabelStatus.Text = String.Format("Thông báo: {0}", Value);
        }
    }
}